package com.ia.aistream.manager.dxfcomponent.util;

import org.apache.ibatis.jdbc.Null;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/****
 * 实现图幅边框的识别
 * @author Ymj
 */
public class DrawBorder {




    public synchronized static List<String> readDxfDataSource(String filePath) throws IOException {

        //通过NIO的方式，将所有的数据读取到内存中
        List<String> fileStr = Files.readAllLines(Paths.get(filePath));
        //将读取到的文件数据返回
        return fileStr;
    }

    // 处理读取文件的类型
    private static Boolean readType(String str){
        if("MTEXT".equals(str) || "TEXT".equals(str)){
            return true;
        }
        return false;
    }

    private static String[] getDrawSize(List<String> list){


        boolean start = false;
        boolean flag = false;
        DocLetter docLetter  = null;
        List<DocLetter> letterList = new ArrayList<>();
        List<DocLetter> numList = new ArrayList<>();

        for(int i=0; i<list.size(); i++){
            String s = list.get(i);
            // 获取H、G的X轴坐标
            // 获取7、8的Y轴坐标
            if("ENTITIES".equals(s)){
                start = true;
            }
            if ("OBJECTS".equals(s)){
                start = false;
            }
            // 实现数据获取
            if(start) {
                if ("  0".equals(s) && readType(list.get(i+1))) {
                    flag = true;
                    docLetter = new DocLetter();
                    continue;
                }
                // 获取X坐标
                if (flag && " 10".equals(s)){
                    docLetter.setLocX(list.get(i+1));
                    continue;
                }
                // 获取Y坐标
                if (flag && " 20".equals(s)){
                    docLetter.setLocY(list.get(i+1));
                    continue;
                }
                // 获取内容
                if (flag && "  1".equals(s)){
                    String ss = list.get(i + 1);
                    docLetter.setContent(ss);
                    flag = false;
                    // zimu
                    if(isOnlyAHLetter(ss)){
                        letterList.add(docLetter);
                    }else if (isOnlyOneNum(ss)){
                        numList.add(docLetter);
                    }
                    docLetter = null;
                    continue;
                }
            }
        }

        System.out.println(numList + "-----");
        System.out.println(letterList);
        getLetterTest(letterList);
        return null;
    }

    /***
     * 实现A-H坐标大小的获取
     */
    public static void getLetter(List<DocLetter> strList){

        // 说明符合正规的图框
        if(strList.size()<=16){
            for (DocLetter d1:strList){
                if (d1.getContent().equals("A")){

                }
            }
        }
        for (DocLetter docLetter : strList){

        }

        String[] str = new String[7];

//        double width = Math.abs(Double.parseDouble(hX) - Double.parseDouble(aX));
//        // 处理横坐标的情况
//        if(xAbs < 0.1 * width){
//            str[2] = y8;
//            str[3] = y7;
//            str[5] = y1;
//            str[0] = hX;
//            str[1] = gX;
//            str[4] = aX;
//            str[6] = "H";
////                str[7] = countX;
////                str[8] = countY;
//        }else{
//            str[2] = x8;
//            str[3] = x7;
//            str[5] = x1;
//            str[0] = hY;
//            str[1] = gY;
//            str[4] = aY;
//            str[6] = "S";
////                str[7] = countY;
////                str[8] = countX;
//        }
    }

    /**
     * 1-8坐标的获取
     * @param numList
     */
    public static void getNum(List<DocLetter> numList){

    }

    /**********************************   工具方法  *************************************/
    /*** 判断内容是否为数字 **/
    private static boolean isOnlyOneNum(String msg){
        String reg = "^[1-8]{1}$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        return matcher.matches();
    }

    /*** 判断内容是否为数字 **/
    private static boolean isOnlyAHLetter(String msg){
        String reg = "^[A-H]{1}$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(msg);

        return matcher.matches();
    }

    /** 实现获取两个对象 **/
    private static int compareNum(DocLetter d1, DocLetter d2){
        int nx1 = 0;
        int nx2 = 0;
        String x1 = d1.getLocX();
        String x2 = d2.getLocX();

        String y1 = d1.getLocY();
        String y2 = d2.getLocY();

        
        if (x1.contains(".")){
            x1 = x1.substring(0, x1.indexOf("."));
        }
        if (x2.contains(".")){
            x2 = x2.substring(0, x2.indexOf("."));
        }

        if (y1.contains(".")){
            y1 = y1.substring(0, y1.indexOf("."));
        }
        if (y2.contains(".")){
            y2 = y2.substring(0, y2.indexOf("."));
        }

        int min = Math.min(x1.length(), x2.length());
        for (int i=0; i<min; i++){
            if (x1.charAt(i)==x2.charAt(i)){
                nx1 = i+1;
            }else{
                break;
            }
        }

        int minY = Math.min(y1.length(), y2.length());
        for (int i=0; i<minY; i++){
            if (y1.charAt(i)==y2.charAt(i)){
                nx2 = i+1;
            }else{
                break;
            }
        }

        // 横纵坐标都不匹配
        if (nx1==0 && nx2==0){
            return 0;
        }else if (nx1>nx2){
            // 哼向看纵坐标
            // 横向
            return 1;
        }else {
            // 纵向
            return 2;
        }
    }


    public static void main(String[] args) throws IOException {
        String dxfPath = "D:\\aa\\tesst.dxf";
//        // 获取dxf文件中的原始数据
        List<String> stringList = readDxfDataSource(dxfPath);
        getDrawSize(stringList);

    }

    public static void getLetterTest(List<DocLetter> strList){

        // 实现数据的拼接
        // [DocLetter(content=A, locX=777.6249990521737, locY=3.00000075579419),
        // DocLetter(content=A, locX=777.6249990521737, locY=587.0000007557942)]
        List<DocLetter> aList = new ArrayList<>();
        List<DocLetter> bList = new ArrayList<>();
        List<DocLetter> cList = new ArrayList<>();
        List<DocLetter> dList = new ArrayList<>();
        List<DocLetter> eList = new ArrayList<>();
        List<DocLetter> fList = new ArrayList<>();
        List<DocLetter> gList = new ArrayList<>();
        List<DocLetter> hList = new ArrayList<>();
        Object[] object = new Object[8];

        for (DocLetter dA:strList){
            if(dA.getContent().equals("A")){
                aList.add(dA);
            }else if(dA.getContent().equals("B")){
                bList.add(dA);
            }else if(dA.getContent().equals("C")){
                cList.add(dA);
            }else if(dA.getContent().equals("D")){
                dList.add(dA);
            }else if(dA.getContent().equals("E")){
                eList.add(dA);
            }else if(dA.getContent().equals("F")){
                fList.add(dA);
            }else if(dA.getContent().equals("G")){
                gList.add(dA);
            }else if(dA.getContent().equals("H")){
                hList.add(dA);
            }
        }

        int type  = -1;
        // 识别A是横向还是纵向
        for (int i=0; i<aList.size(); i++){
            for (int j=i+1; j<aList.size(); j++){
                int num = compareNum(aList.get(i), aList.get(j));
                if(num==1){
                    // 横向数据
                    type = 2;
                    break;
                }else if(num==2) {
                    // 纵向数据
                    type = 1;
                    break;
                }
            }
        }
        System.out.println(type);
        for(DocLetter da: aList){
            object[0] = da;
            for (DocLetter db: bList){
                object[1] = db;
                int i = compareNum(da, db);
                if (i==type) {
                    for (DocLetter dc : cList) {
                        object[2] = dc;
                        int i1 = compareNum(db, dc);
                        if (i1==type){
                            for (DocLetter dd : dList) {
                                object[3] = dd;
                                int i2 = compareNum(dc, dd);
                                if (i2==type){
                                    for (DocLetter de:eList){
                                        object[4] = de;
                                        int i3 = compareNum(dd, de);
                                        if(i3==type){
                                            for (DocLetter df:fList){
                                                object[5] = df;
                                                int i4 = compareNum(de, df);
                                                if(i4==type && gList.size()>0){
                                                    for (DocLetter dg:gList){
                                                        object[6] = dg;
                                                        int i5 = compareNum(df, dg);
                                                        if(i5==type && hList.size()>0){
                                                            for (DocLetter dh:hList){
                                                                object[7] = dh;
                                                                int i6 = compareNum(dg, dh);
                                                                if(i6==type){
                                                                    System.out.println(da + "----" + db + "-----" + dc + "------" + dd + "---" + de + "---" + df + "---" + dg + "-----" + dh);
                                                                    return;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
